import { getMongoRepository } from 'typeorm'
import { Courses } from '../models/Course'
import { isEmpty } from '../utils/index'

class CourseService {

  // 根据 id 查询
  findById(id: string) {
    const courseRespository = getMongoRepository(Courses)
    return courseRespository.findOne(id)
  }

  // 根据课程、教师、学院、专业名称模糊查询
  findByNameKey(label: string, value: string, skipNum: number, limit: number, teacher_id?: string) {
    const courseRespository = getMongoRepository(Courses)
    // console.log('teacher_id:',teacher_id);
    // console.log(label, value, skipNum, limit);
    // 如果传teacher_id，说明是教师请求，否则是管理员请求
    if (!isEmpty(teacher_id)) {
      return courseRespository.find({
        where: {
          teacher_id: teacher_id,
          [label]: { $regex: value }
        },
        skip: skipNum,
        take: limit
      })
    } else {
      return courseRespository.find({
        where: {
          [label]: { $regex: value }
        },
        skip: skipNum,
        take: limit
      })
    }
  }

  // 根据课程、教师、学院、专业名称模糊查询，总数量
  async findByNameKeyCount(label: string, value: string, teacher_id?: string) {
    const courseRespository = getMongoRepository(Courses);
    if (!isEmpty(teacher_id)) {
      const [users, count] = await courseRespository.findAndCount({
        where: {
          teacher_id: teacher_id,
          [label]: { $regex: value }
        }
      })
      return count
    } else {
      const [users, count] = await courseRespository.findAndCount({
        where: {
          [label]: { $regex: value }
        }
      })
      return count
    }
  }

  // 添加课程
  createCourse(course: Courses) {
    const courseRespository = getMongoRepository(Courses)
    return courseRespository.save(course)
  }

  // 根据课程名称查询
  findByCourseName(course_name: string) {
    const courseRespository = getMongoRepository(Courses)
    return courseRespository.findOne({ course_name })
  }

  // 修改信息
  updateCourseInfo(id: string, course: Courses) {
    const courseRespository = getMongoRepository(Courses);
    // delete admin.id
    return courseRespository.update(id, course)
  }

  // 修改课程状态
  udpateCourseState(id: string, state: boolean) {
    const courseRespository = getMongoRepository(Courses)
    return courseRespository.update(id, { state })
  }

  // 删除
  deleteCourse(id: string) {
    const courseRespository = getMongoRepository(Courses)
    return courseRespository.delete(id)
  }

  // 学生主修选修的查询
  findByStuUseNameKey(label: string, value: string, skipNum: number, limit: number, type: number) {
    const courseRespository = getMongoRepository(Courses)
    return courseRespository.find({
      where: {
        type,
        [label]: { $regex: value }
      },
      skip: skipNum,
      take: limit
    })
  }
  // 根据课程、教师、学院、专业名称模糊查询，总数量
  async findByStuUseNameKeyCount(label: string, value: string, type: number) {
    const courseRespository = getMongoRepository(Courses);
    const [users, count] = await courseRespository.findAndCount({
      where: {
        type,
        [label]: { $regex: value }
      }
    })
    return count
  }

  // 更新剩余可选人数
  updateCourseRemaining(id: string, remaining: number) {
    const courseRespository = getMongoRepository(Courses);
    return courseRespository.update(id, { remaining })

  }
}

export default new CourseService()
